<?php
include("include/config.inc.php");

if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	$logDataArray = 0;
	$incDtlArray  = 0;
	$dtlTwoArray  = 0;
	if(isset($_GET['pmSubmitBtn']))
	{
		$pmDate = $_GET['pmDateYear']."-".$_GET['pmDateMonth']."-".$_GET['pmDateDay'];
		$startTime = date('Y-m-d')." ".$_GET['startTimeHour'].":".$_GET['startTimeMinute'].":00";
		$soakingStartTime = date('Y-m-d')." ".$_GET['soakStartTimeHour'].":".$_GET['soakStartTimeMinute'].":00";
		$endTime = date('Y-m-d')." ".$_GET['endTimeHour'].":".$_GET['endTimeMinute'].":00";
		$extraTempAttain  = date('Y-m-d')." ".$_GET['extraTempAttainHour'].":".$_GET['extraTempAttainMinute'].":00";
		$extraSoaking     = date('Y-m-d')." ".$_GET['extraSoakingHour'].":".$_GET['extraSoakingMinute'].":00";
		$extraOut         = date('Y-m-d')." ".$_GET['extraOutHour'].":".$_GET['extraOutMinute'].":00";

		if(isset($_GET['putExtraData']) && $_GET['putExtraData'] > 0)
		{
  	  $extraUpdateQuery = "putExtraData = ".$_GET['putExtraData'].",extraTempAttain = '".$extraTempAttain."',
  	                       extraTempAttainTemperature = '".$_GET['extraTempAttainTemperature']."',
  	                       extraSoaking = '".$extraSoaking."',
  	                       extraSoakingTemperature = '".$_GET['extraSoakingTemperature']."',
  	                       extraOut = '".$extraOut."',
  	                       extraOutTemperature = '".$_GET['extraOutTemperature']."',";
    }
    else
    {
    	$extraUpdateQuery = " putExtraData = 0,";
    }
		
		$updatePMMaster = "UPDATE pmmaster
		                      SET pmDate = '".$pmDate."',newPartyName ='".$_GET['newPartyName']."',
		                          newProcessName = '".$_GET['newProcessName']."',materialId = ".$_GET['materialId'].",
		                          startTime = '".$startTime."',startTemperature = '".$_GET['startTemperature']."',
		                          soakingStartTime = '".$soakingStartTime."', soakingStartTemperature = '".$_GET['soakingStartTemperature']."',
		                          soakingHour = '".$_GET['soakingHour']."',soakingHourTemperature = '".$_GET['soakingHourTemperature']."',
		                          endTime = '".$endTime."',endTemperature = '".$_GET['endTemperature']."',
		                          qMId = ".$_GET['qMId'].",quenchingTime = ".$_GET['quenchingTime'].",tempBforQue = ".$_GET['tempBforQue'].",
		                          tempAfterQue = ".$_GET['tempAfterQue'].",".$extraUpdateQuery." notes = '".nl2br(addslashes($_GET['notes']))."'
		                    WHERE pmMasterId = ".$_GET['pmMasterId'];
		$updatePMMasterRes = mysql_query($updatePMMaster);
		if($updatePMMasterRes)
		{
			$deleteDtlOne = "DELETE FROM pmdetailone WHERE pmMasterId = ".$_GET['pmMasterId'];
			$deleteDtlOneRes = mysql_query($deleteDtlOne);
			foreach($_GET['IncomingDetailId'] AS $keyId => $values)
			{
			  $insPMDetailone = "INSERT INTO pmdetailone(pmMasterId, columns1, columns2, columns3, columns4, columns5, columns6)
			                     VALUES(".$_GET['pmMasterId'].",'".$_GET['columns1'][$keyId]."','".$_GET['columns2'][$keyId]."',
			                            '".$_GET['columns3'][$keyId]."','".$_GET['columns4'][$keyId]."','".$_GET['columns5'][$keyId]."',
			                            '".$_GET['columns6'][$keyId]."')";
			  $insPMDetailoneRes = mysql_query($insPMDetailone);
			}
		}
		if($updatePMMasterRes)
		{
			$deleteDtlTwo = "DELETE FROM pmdetailtwo WHERE pmMasterId = ".$_GET['pmMasterId'];
			$deleteDtlTwoRes = mysql_query($deleteDtlTwo);			
			$j = 0;
      for($i=0; $i < count($_GET['detailTime']); $i++)
			{
				if(isset($_GET['detailTime'][$i]['Time_Hour']))
				{
					$detailTime = "";
					$detailTime = date('Y-m-d')." ".$_GET['detailTime'][$i]['Time_Hour'];
				}
				if(isset($_GET['detailTime'][$i]['Time_Minute']))
				{
					$detailTime .= ":".$_GET['detailTime'][$i]['Time_Minute'].":00";
					$insPMDetailtwo = "INSERT INTO pmdetailtwo(pmMasterId, detailTime, detailTemperature)
					                   VALUES (".$_GET['pmMasterId'].",'".$detailTime."','".$_GET['detailTemperature'][$j]."')";
          $insPMDetailtwoRes = mysql_query($insPMDetailtwo);
					$j++;
				}
		  }
		}
		
		if(isset($_REQUEST['incomingId']) && $_REQUEST['logSheetId'] > 0)
		{  
			$column1 = (isset($_REQUEST['column1'])) ? $_REQUEST['column1'] : "";
			$column2 = (isset($_REQUEST['column2'])) ? $_REQUEST['column2'] : "";
			$column3 = (isset($_REQUEST['column3'])) ? $_REQUEST['column3'] : "";
			$column4 = (isset($_REQUEST['column4'])) ? $_REQUEST['column4'] : "";
			$column5 = (isset($_REQUEST['column5'])) ? $_REQUEST['column5'] : "";
			$column6 = (isset($_REQUEST['column6'])) ? $_REQUEST['column6'] : "";
			
			$updateIncomeing = "UPDATE incoming 
			                       SET column1    = '".$column1."',
			                           column2    = '".$column2."',
			                           column3    = '".$column3."',
			                           column4    = '".$column4."',
			                           column5    = '".$column5."',
			                           column6    = '".$column6."'
			                     WHERE incomingId = ".$_REQUEST['incomingId'];
		  $updateIncomeingRes = mysql_query($updateIncomeing);
		  if(!$updateIncomeingRes)
		  {
		  	echo "Update Fail";
		  }
		  else
		  {
				foreach($_GET['IncomingDetailId'] AS $keyId => $values)
				{
					$deleteDtlOne = "DELETE FROM incomingdetail WHERE IncomingDetailId = ".$keyId;
				  $deleteDtlOneRes = mysql_query($deleteDtlOne);
				  $insPMDetailone = "INSERT INTO incomingdetail(incomingId, columns1, columns2, columns3, columns4, columns5, columns6)
				                     VALUES(".$_GET['incomingId'].",'".$_GET['columns1'][$keyId]."','".$_GET['columns2'][$keyId]."',
				                            '".$_GET['columns3'][$keyId]."','".$_GET['columns4'][$keyId]."','".$_GET['columns5'][$keyId]."',
				                            '".$_GET['columns6'][$keyId]."')";
				  $insPMDetailoneRes = mysql_query($insPMDetailone);
				}
		  	header("Location:materialReport.php");
		  }
		}
		if($updatePMMasterRes)
		{
			$updateLGSheet = "UPDATE logsheet
			                     SET pmrDone = 'Y'
			                   WHERE logSheetId = ".$_GET['logSheetId'];
      $updateLGSheetRes = mysql_query($updateLGSheet);
	  }
	  header('Location:materialReport.php');
	}
	
	if(isset($_GET['logSheetId']) && $_GET['logSheetId'] > 0)
	{
		$selectData = "SELECT pmMasterId,DATE_FORMAT(pmDate,'%d-%m-%Y') AS pmDate,pmmaster.incomingId,newPartyName,
		                      pmmaster.planningId,batchNo,logSheetId,pmmaster.processId,newProcessName,quantity,
		                      pmmaster.weight,pmmaster.materialId,DATE_FORMAT(startTime,'%H:%i') AS startTime,
		                      startTemperature,DATE_FORMAT(soakingStartTime,'%H:%i') AS soakingStartTime,
		                      soakingStartTemperature,soakingHour,soakingHourTemperature,
		                      DATE_FORMAT(endTime,'%H:%i') AS endTime,endTemperature,qMId,quenchingTime,tempBforQue,
		                      tempAfterQue,putExtraData,extraTempAttain,extraTempAttainTemperature,extraSoaking,
		                      extraSoakingTemperature,extraOut,extraOutTemperature, notes,incoming.challan,
		                      column1,column2,column3,column4,column5,column6,partyName,processName
		                 FROM pmmaster
		                 JOIN incoming ON pmmaster.incomingId = incoming.incomingId
		                 JOIN party    ON incoming.partyId = party.partyId
		                 JOIN process  ON pmmaster.processId = process.processId
		                WHERE logSheetId = ".$_GET['logSheetId']."
		                  AND pmmaster.incomingId = ".$_GET['incomingId'];
    $selectDataRes = mysql_query($selectData);
    $logDataArray = array();
    if($logRow = mysql_fetch_array($selectDataRes))
    {
    	$logDataArray['pmMasterId']       = $logRow['pmMasterId'];
    	$logDataArray['logSheetId']       = $logRow['logSheetId'];
    	$logDataArray['incomingId']       = $logRow['incomingId'];
    	$logDataArray['planningId']       = $logRow['planningId'];
    	$logDataArray['batchNo']          = $logRow['batchNo'];
    	$logDataArray['processId']        = $logRow['processId'];
    	$logDataArray['pmDate']           = $logRow['pmDate'];
    	$logDataArray['startTime']        = $logRow['startTime'];
    	$logDataArray['startTemperature'] = $logRow['startTemperature'];
    	$logDataArray['soakingStartTime'] = $logRow['soakingStartTime'];
    	$logDataArray['soakingStartTemperature'] = $logRow['soakingStartTemperature'];
    	$logDataArray['soakingHour']      = $logRow['soakingHour'];
    	$logDataArray['soakingHourTemperature'] = $logRow['soakingHourTemperature'];
    	$logDataArray['endTime']          = $logRow['endTime'];
    	$logDataArray['endTemperature']   = $logRow['endTemperature'];
    	$logDataArray['qMId']             = $logRow['qMId'];
    	$logDataArray['quenchingTime']    = $logRow['quenchingTime'];
    	$logDataArray['tempBforQue']      = $logRow['tempBforQue'];
    	$logDataArray['tempAfterQue']     = $logRow['tempAfterQue'];
    	
    	$logDataArray['putExtraData']     = $logRow['putExtraData'];
    	$logDataArray['extraTempAttain']  = $logRow['extraTempAttain'];
    	$logDataArray['extraTempAttainTemperature'] = $logRow['extraTempAttainTemperature'];
    	$logDataArray['extraSoaking']     = $logRow['extraSoaking'];
    	$logDataArray['extraSoakingTemperature'] = $logRow['extraSoakingTemperature'];
    	$logDataArray['extraOut']         = $logRow['extraOut'];
    	$logDataArray['extraOutTemperature']= $logRow['extraOutTemperature'];
    	
    	$logDataArray['incChallan']       = $logRow['challan'];
    	$logDataArray['materialId']       = $logRow['materialId'];
    	$logDataArray['column1']          = $logRow['column1'];
    	$logDataArray['column2']          = $logRow['column2'];
    	$logDataArray['column3']          = $logRow['column3'];
    	$logDataArray['column4']          = $logRow['column4'];
    	$logDataArray['column5']          = $logRow['column5'];
    	$logDataArray['column6']          = $logRow['column6'];
    	$logDataArray['quantity']         = $logRow['quantity'];
    	$logDataArray['weight']           = $logRow['weight'];
    	$logDataArray['partyName']        = $logRow['partyName'];
    	$logDataArray['newPartyName']     = $logRow['newPartyName'];
    	$logDataArray['processName']      = $logRow['processName'];
    	$logDataArray['newProcessName']   = $logRow['newProcessName'];
    	$logDataArray['notes']            = nl2br($logRow['notes']);
    	
    	$selIncDtl = "SELECT IncomingDetailId,columns1,columns2,columns3,columns4,columns5,columns6
    	                FROM incomingdetail
    	               WHERE IncomingId = ".$logRow['incomingId'];
      $selIncDtlRes = mysql_query($selIncDtl);
      $incDtlArray  = array();
      $i = 0;
      while($incDtlRow = mysql_fetch_array($selIncDtlRes))
      {
      	$incDtlArray[$i]['IncomingDetailId'] = $incDtlRow['IncomingDetailId'];
      	
      	$selPMDetailTwo = "SELECT * FROM pmdetailone WHERE IncomingDetailId = ".$incDtlRow['IncomingDetailId'];
      	$selPMDetailTwoRes = mysql_query($selPMDetailTwo);
      	if($pmdDtlRow = mysql_fetch_array($selPMDetailTwoRes))
      	{
        	$incDtlArray[$i]['columns1'] = $pmdDtlRow['columns1'];
        	$incDtlArray[$i]['columns2'] = $pmdDtlRow['columns2'];
        	$incDtlArray[$i]['columns3'] = $pmdDtlRow['columns3'];
        	$incDtlArray[$i]['columns4'] = $pmdDtlRow['columns4'];
        	$incDtlArray[$i]['columns5'] = $pmdDtlRow['columns5'];
        	$incDtlArray[$i]['columns6'] = $pmdDtlRow['columns6'];
        	$i++;
      	}
      	else
      	{
        	$incDtlArray[$i]['columns1'] = $incDtlRow['columns1'];
        	$incDtlArray[$i]['columns2'] = $incDtlRow['columns2'];
        	$incDtlArray[$i]['columns3'] = $incDtlRow['columns3'];
        	$incDtlArray[$i]['columns4'] = $incDtlRow['columns4'];
        	$incDtlArray[$i]['columns5'] = $incDtlRow['columns5'];
        	$incDtlArray[$i]['columns6'] = $incDtlRow['columns6'];
        	$i++;
      	}
      }
      
      $selPMDetailTwo = "SELECT DATE_FORMAT(detailTime,'%H:%i') AS detailTime, detailTemperature
                           FROM pmdetailtwo
                          WHERE pmMasterId = ".$logRow['pmMasterId'];
      $selPMDetailTwoRes = mysql_query($selPMDetailTwo);
      $dtlTwoArray = array();
      $k = 0;
      while($dtlTwo = mysql_fetch_array($selPMDetailTwoRes))
      {
      	$dtlTwoArray[$k]['detailTime'] = $dtlTwo['detailTime'];
      	$dtlTwoArray[$k]['detailTemperature'] = $dtlTwo['detailTemperature'];
      	$k++;
      }
    }
	}
	//---------------------material---------------------------
  $m = 0;                                                                     
  $select = "SELECT materialId,materialName                                    
	             FROM material
	            ORDER BY materialName";                                                   
	$selectResult = mysql_query($select);                                       
	while($row = mysql_fetch_array($selectResult))                              
	{                                                                           
	  $catmaterialArray['materialId'][$m]   = $row['materialId'];                    
	  $catmaterialArray['materialName'][$m] = $row['materialName'];                      
	  $m++;                                                                     
	}                    
	//---------------------Quencing Media---------------------------
	$n = 0;                                                                     
	$selectQ = "SELECT qMId,qMName                                    
	              FROM quenchingmedia
	             ORDER BY qMName";                                                   
	$selectQResult = mysql_query($selectQ);                                       
	while($qrow = mysql_fetch_array($selectQResult))                              
	{                                                                           
	  $qmArray['qMId'][$n]   = $qrow['qMId'];                    
	  $qmArray['qMName'][$n] = $qrow['qMName'];                      
	  $n++;                                                                     
	}                    
	
	include("./bottom.php");
  $smarty->assign("catmaterialArray",$catmaterialArray);
	$smarty->assign("qmArray",$qmArray);
	$smarty->assign("logDataArray",$logDataArray);
	$smarty->assign("incDtlArray",$incDtlArray);
	$smarty->assign("dtlTwoArray",$dtlTwoArray);
	$smarty->display("processMonitoringEdit.tpl");
}
?>